import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

typedef SessionHeaderAction = void Function(int sessionIndex);
typedef SessionItemAction = void Function(int sessionIndex, int itemIndex);
typedef GroupSessionHeaderBuilder = Widget Function( BuildContext context, {@required int sessionIndex, SessionHeaderAction onTap});
typedef GroupSessionItemBuilder = Widget Function(BuildContext context, {@required int sessionIndex,@required int itemIndex, SessionItemAction onTap});

class GroupListView extends StatefulWidget {
  GroupListViewBuilder groupListViewBuilder;
  GroupListView({@required this.groupListViewBuilder});
  @override
  _GroupListViewState createState() => _GroupListViewState();
}

class _GroupListViewState extends State<GroupListView> {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      key: widget.groupListViewBuilder.key,
      scrollDirection:widget.groupListViewBuilder.scrollDirection ,
      reverse: widget.groupListViewBuilder.reverse,
      controller: widget.groupListViewBuilder.controller,
      primary: widget.groupListViewBuilder.primary,
      physics: widget.groupListViewBuilder.physics,
      shrinkWrap: widget.groupListViewBuilder.shrinkWrap,
      padding: widget.groupListViewBuilder.padding,
      itemExtent: widget.groupListViewBuilder.itemExtent,
      itemBuilder: _itemBuilder,
      itemCount: widget.groupListViewBuilder.sessionCount,
      addAutomaticKeepAlives: widget.groupListViewBuilder.addAutomaticKeepAlives,
      addRepaintBoundaries: widget.groupListViewBuilder.addRepaintBoundaries,
      addSemanticIndexes: widget.groupListViewBuilder.addSemanticIndexes,
      cacheExtent: widget.groupListViewBuilder.cacheExtent,
      semanticChildCount: widget.groupListViewBuilder.semanticChildCount,
      dragStartBehavior: widget.groupListViewBuilder.dragStartBehavior,
    );
  }

  Widget _itemBuilder(BuildContext context, int sessionIndex) {
    return _SessionView(sessionIndex, widget.groupListViewBuilder);
  }
}

class _SessionView extends StatefulWidget {
  int sessionIndex;
  GroupListViewBuilder groupListViewBuilder;

  _SessionView(this.sessionIndex, this.groupListViewBuilder);

  @override
  _SessionViewState createState() => _SessionViewState();
}

class _SessionViewState extends State<_SessionView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          widget.groupListViewBuilder.sessionHeaderBuilder(
              context,
              sessionIndex:widget.sessionIndex,
              onTap:(sessionIndex){
                setState(() {

                });
              }
          ),
          ListView.builder(
              itemCount: widget.groupListViewBuilder.itemOfSessionCount(widget.sessionIndex),
              shrinkWrap: true,/*内嵌listView 这个值要设置成true*/
              primary: true,
              physics: NeverScrollableScrollPhysics(),
              itemBuilder: (context, itemIndex){
                return widget.groupListViewBuilder.sessionItemBuilder(
                    context,
                    sessionIndex:widget.sessionIndex,
                    itemIndex:itemIndex,
                    onTap:(sessionIndex,itemIndex){
                      setState(() {

                      });
                    }
                );
              }
          ),
        ],
      ),
    );
  }
}

class GroupListViewBuilder {
  Key key;
  Axis scrollDirection;
  bool reverse;
  ScrollController controller;
  bool primary;
  ScrollPhysics physics;
  bool shrinkWrap;
  EdgeInsetsGeometry padding;
  double itemExtent;
  GroupSessionHeaderBuilder sessionHeaderBuilder;
  GroupSessionItemBuilder sessionItemBuilder;
  int sessionCount;
  final int Function(int section) itemOfSessionCount;
  bool addAutomaticKeepAlives;
  bool addRepaintBoundaries;
  bool addSemanticIndexes;
  double cacheExtent;
  int semanticChildCount;
  DragStartBehavior dragStartBehavior;
  Function(int sessionIndex) sessionHeaderOnTap;

  GroupListViewBuilder({
    this.key,
    @required this.sessionHeaderBuilder,
    @required this.sessionItemBuilder,
    this.scrollDirection = Axis.vertical,
    this.reverse = false,
    this.controller,
    this.primary,
    this.physics,
    this.shrinkWrap = false,
    this.padding,
    this.itemExtent,
    this.sessionCount,
    this.itemOfSessionCount,
    this.addAutomaticKeepAlives = true,
    this.addRepaintBoundaries = true,
    this.addSemanticIndexes = true,
    this.cacheExtent,
    this.semanticChildCount,
    this.dragStartBehavior = DragStartBehavior.start,
    this.sessionHeaderOnTap
  });
}
